<?php
namespace app\admin\controller;

use app\common\model\ZhSupportReply as ZhSupportReplyModel;
use app\common\model\ZhSupportReplyEducation as ZhSupportReplyEducationModel;

/**
 * 答疑解惑控制器
 * Class Zhsupport
 * @package app\index\controller
 */
class Zhsupportreply extends Base
{
    /**
     * [index  列表]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $rmodel = new ZhSupportReplyModel;
        $emodel = new ZhSupportReplyEducationModel;
        $this->getPageAndSize($param);
        $map = [];
        try {
            $map['s.sid'] = $param['id'];
            $count = $rmodel->getCounts($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $rmodel->getlists($map, $this->from, $this->size,'s.id,s.status,s.content,s.create_time,m.username,m.img,m.sex');
            foreach($lists as &$v){
                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    $v['img'] = config('domain_name').$v['img'];
                }
                $v['education_num'] = $emodel->getAllCount(['rid'=>$v['id']]);
            }
        }catch(\Exception $e){
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功', [
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [index  列表]
     * @return json
     * @author [C。]
     */
    public function status()
    {
        $param = input('param.');
        $rmodel = new ZhSupportReplyModel;
        try{
            $reply = $rmodel->findData($param['id'],'id','status');
            if($reply['status'] == 1){
                $rmodel->editData(['id'=>$param['id'],'status'=>2]);
                $name = '禁用';
            }elseif($reply['status'] == 2){
                $rmodel->editData(['id'=>$param['id'],'status'=>1]);
                $name = '启用';
            }
        }catch(\Exception $e){
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, $name.'成功');
    }
}